<template>
  <div class="app-container" style="margin: 10px" >
    <el-row :gutter="10" class="mb8">
      <el-col :span="5">
        文献、指南和共识处方知识库
      </el-col>
      <el-col :span="1.5">
        <el-input v-model="queryParams.search" placeholder="请输入关键字" />
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" >
      <el-table-column label="二便障碍类型" align="center" prop="two_type" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="病症/严重程度" align="center" prop="symptoms" />
      <el-table-column label="禁忌症" align="center" prop="contraindications" />
      <el-table-column label="处方类型" align="center" prop="prescription_type" />
      <el-table-column label="处方详情" align="center" prop="details" />
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" >修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.page_size"
        @pagination="getList"
    />

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="dataRef" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="二便障碍类型：" prop="two_type">
          <el-select
              v-model="form.two_type" collapse-tags collapse-tags-tooltip
              :max-collapse-tags="2" value-key="type_name" placeholder="请选择障碍类型" class="all" >
            <el-option
                v-for="item in options"
                :key="item.type_name"
                :label="item.type_name"
                :value="item.type_name"
            />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="序号" prop="id">-->
<!--          <el-input v-model="form.id" placeholder="请输入序号" />-->
<!--        </el-form-item>-->
        <el-form-item label="病症/严重程度" prop="symptoms">
          <el-input v-model="form.symptoms" placeholder="请输入病症/严重程度" />
        </el-form-item>
        <el-form-item label="禁忌症" prop="contraindications">
          <el-input v-model="form.contraindications" placeholder="禁忌症" />
        </el-form-item>
        <el-form-item label="处方类型" prop="prescription_type">
          <el-input v-model="form.prescription_type" placeholder="处方类型" />
        </el-form-item>
        <el-form-item label="处方详情" prop="details">
          <el-input v-model="form.details" placeholder="处方详情" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="patientData">
import { disordersType } from '@/api/login'
import { listK1, addK1, editK1, deleteK1 } from '@/api/v2'

import {reactive} from "vue";

const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const options = reactive([])

disordersType().then(res => {
  res.forEach(e => {
    options.push(e);
  });
})
const data = reactive({
  form: {},
  queryParams: {
    search: undefined,
    page_size:10
  },
  rules: {
    two_type: [{ required: true, message: "障碍类型不能为空", trigger: ["blur", "change"] }],
    // xh: [{ required: true, message: "序号不能为空", trigger: ["blur", "change"] }],
    symptoms: [{ required: true, message: "病症/严重程度不能为空", trigger: ["blur", "change"] }],
    contraindications: [{ required: true, message: "禁忌症不能为空", trigger: ["blur", "change"] }],
    prescription_type: [{ required: true, message: "处方类型不能为空", trigger: ["blur", "change"] }],
    details: [{ required: true, message: "处方详情不能为空", trigger: ["blur", "change"] }],
  },
});
const { queryParams, form, rules } = toRefs(data);


/** 查询参数列表 */
function getList() {
  loading.value = true;
  listK1(queryParams.value).then(response => {
    // dataList.value = response
    dataList.value = response.results;
    total.value = response.count;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}

/** 表单重置 */
function reset() {
  form.value = {
    id:undefined,
    two_type: undefined,
    xh: undefined,
    symptoms: undefined,
    contraindications: undefined,
    prescription_type: undefined,
    details: undefined
  };
  proxy.resetForm("dataRef");
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加知识";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = "修改知识";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        editK1(form.value.id,form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addK1(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id;
  proxy.$modal.confirm('是否确认删除编号为"' + id + '"的知识？').then(function () {
    return deleteK1(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>

<style scoped>

</style>
